<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015-8-25
 * Time: 10:13
 */

namespace common\components\services\timedtask;

use common\models\datas\Appointment;
use common\models\datas\AttendClass;
use common\components\helpers\Fun;
use yii\base\Component;

class Appointments extends Component
{
   //统计预约
    public function send()
    {
        $yesterdayTime = strtotime(date("Y-m-d",strtotime("-1 day")));//前一天开始时间戳
        $todayTime = strtotime(date("Y-m-d"));//前一天结束时间戳
        $site_cache  = Fun::cache('site_cache',false);
        if($site_cache != null) {
            foreach($site_cache as $key=>$site) {
                if(isset($site['db'])) {
                    $orderSales = (new \yii\db\Query())
                                  ->select(['os.order_id','os.category_id','os.site_id','os.id','os.created_at'])
                                  ->from('{{%order_sales}} os')
                                  ->leftJoin('{{%order}} o','o.id = os.order_id')
                                  ->where(['o.is_appointment'=>1])
                                  ->andWhere(['between', 'os.created_at', $yesterdayTime, $todayTime])
                                  ->all(\Yii::createObject($site['db']));
                    if ($orderSales != null) {
                        foreach ($orderSales as $val) {
                            $model = new Appointment();
                            $model->order_id = $val['order_id'];
                            $model->category_id = $val['category_id'];
                            $model->site_id = $val['site_id'];
                            $model->order_sales_id = $val['id'];
                            $model->appointment_time = $val['created_at'];
                            $model->created_at = time();
                            if ($model->save(false)) {
                                echo 'success' . time();
                            }
                        }
                    }
                }
            }
        }

    }

    //统计上课人数
    public function attendClass(){

        $yesterdayTime = strtotime(date("Y-m-d",strtotime("-1 day")));//前一天开始时间戳
        $todayTime = strtotime(date("Y-m-d"));//前一天结束时间戳
        $site_cache  = Fun::cache('site_cache',false);
        if($site_cache != null){
            foreach($site_cache as $key=>$site) {
                if(isset($site['db'])) {
                    $ClassStudent = (new \yii\db\Query())->select(['c.*','o.category_id', 'o.site_id'])
                                    ->from('{{%class_student}} c')
                                    ->leftJoin('{{%order_sales}} o', 'c.order_sales_id = o.id')
                                    ->where(['c.status' => 1])
                                    ->andWhere(['between','c.start_time', $yesterdayTime, $todayTime])
                                    ->all(\Yii::createObject($site['db']));
                    if($ClassStudent){
                        foreach($ClassStudent as $val){
                            $model = new AttendClass();
                            $model->order_sales_id = $val['order_sales_id'];
                            $model->category_id    = !empty($val['category_id'])?$val['category_id']:0;
                            $model->site_id        = !empty($val['site_id'])?$val['site_id']:0;
                            $model->start_time     = $val['start_time'];
                            $model->class_student_id = $val['id'];
                            $model->created_at     = time();
                            if ($model->save(false)) {
                                echo 'success' . time();
                            }
                        }

                    }
                }
            }
        }
    }
}